package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import com.itheima.utils.AliOSSUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class EmpServiceImpl implements EmpService {

    private final EmpMapper empMapper;
    private final AliOSSUtils aliOSSUtils;

    /**
     * 根据id查询员工信息
     *
     * @param id
     * @return
     */
    @Override
    public Emp empById(Integer id) {
        return empMapper.empById(id);
    }

    /**
     * 分页查询员工信息
     *
     * @param name
     * @param gender
     * @param page
     * @param pageSize
     * @param begin
     * @param end
     * @return
     */
    @Override
    public PageBean empList(String name, Integer gender, Integer page, Integer pageSize, LocalDateTime begin,
                            LocalDateTime end) {
        // 开启分页     pageNum是当前页码(展示第几页) pageSize是每页显示的条数
        PageHelper.startPage(page, pageSize);
        //拦截分页查询的SQL语句
        List<Emp> empList = empMapper.pageQuery();
        //将结果转为page对象
        Page<Emp> p = (Page<Emp>) empList;
        //返回pageBean    p.getTotal是总记录数 p.getResult()是当前结果集
        return new PageBean(p.getTotal(), p.getResult());
    }

    /**
     * 批量删除员工信息
     *
     * @param ids
     */
    @Override
    public void deleteByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);
    }

    /**
     * 添加员工信息
     *
     * @param emp
     */
    @Override
    public void addEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.addEmp(emp);
    }

    /**
     * 修改员工信息
     *
     * @param emp
     */
    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.dpateEmp(emp);
    }

    /**
     * 登录
     *
     * @return
     */
    @Override
    public Emp login(Emp emp) {
        return empMapper.login(emp);
    }

}
